<template>
	<view class="content">
		<view style="text-align: center;">目前只支持网页版</view>
		<uni-easyinput v-model="music.name" placeholder="歌名"></uni-easyinput>
		<uni-easyinput v-model="music.arname" placeholder="歌手"></uni-easyinput>
		<uni-easyinput v-model="music.alname" placeholder="专辑"></uni-easyinput>
		<view class="buttonview">
			<button style="width: 50%;" @click="getMusicImg()">选择封面文件</button>
			<button style="width: 50%;" @click="getMusicFile()">选择歌曲文件</button>
		</view>
		<view>封面文件：{{musicImgName}}</view>
		<view>歌曲文件：{{musicFileName}}</view>
		<button @click="upmusic()">上传</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'FindCloudMusicList',
				name: '',
				musicList: [],
				musicFileName: '',
				musicImgName: '',
				musicImg: {},
				musicFile: {},
				music: {
					"name": '',
					"arname": '',
					"alname": '',
					"img": '',
					"userid": "1"
				}
			}
		},
		onLoad() {
			this.music.userid = this.Device.deviceId
		},
		methods: {
			async upmusic() {
				if (this.music.name === undefined || this.music.name === null || this.music.name === '') {
					uni.showToast({
						icon: 'error',
						title: '歌名为空',
						duration: 2000
					});
					return
				}
				if (this.music.arname === undefined || this.music.arname === null || this.music.arname === '') {
					uni.showToast({
						icon: 'error',
						title: '歌手为空',
						duration: 2000
					});
					return
				}
				if (this.music.alname === undefined || this.music.alname === null || this.music.alname === '') {
					uni.showToast({
						icon: 'error',
						title: '专辑为空',
						duration: 2000
					});
					return
				}
				if (this.musicFileName === undefined || this.musicFileName === null || this
					.musicFileName === '') {
					uni.showToast({
						icon: 'error',
						title: '歌曲文件为空',
						duration: 2000
					});
					return
				}
				if (this.musicImgName === undefined || this.musicImgName === null || this
					.musicImgName === '') {
					uni.showToast({
						icon: 'error',
						title: '封面为空',
						duration: 2000,
					});
					return
				}
				uni.showLoading({
					title: '封面上传中',
					mask: true
				});
				let res = await this.HttpTool.UpMusicImg(this, this.musicImg.path, this.music)
				uni.hideLoading();
				res = JSON.parse(res)
				if(res.code !== 200){
					uni.showToast({
						icon: 'error',
						title: '封面上传失败',
						duration: 2000,
					});
					return
				}
				this.music.img = res.data
				uni.showLoading({
					title: '歌曲上传中',
					mask: true
				});
				res = await this.HttpTool.UpMusic(this, this.musicFile.path, this.music)
				uni.hideLoading();
				res = JSON.parse(res)
				if(res.code !== 200){
					uni.showToast({
						icon: 'error',
						title: '歌曲上传失败',
						duration: 2000,
					});
					return
				}else{
					uni.showToast({
						title: '歌曲上传成功',
						duration: 2000,
					});
					uni.navigateBack()
				}
				
			},
			async clickMusic(music) {

			},
			async getMusicFile() {
				let that = this
				uni.chooseFile({
					count: 1, //默认100
					extension: ['.mp3', '.flac'],
					success: function(res) {
						that.musicFile = res.tempFiles[0]
						that.musicFileName = res.tempFiles[0].name
						if (that.music.name === undefined || that.music.name === null || that.music
							.name === '') {
							that.music.name = res.tempFiles[0].name.replace('.mp3', '').replace('.flac',
								'')
						}
					}
				});
			},
			async getMusicImg() {
				let that = this
				uni.chooseFile({
					count: 1,
					extension: ['.jpg', '.jpeg', '.png', '.gif'],
					success(res) {
						console.log(res);
						// tempFilePath可以作为img标签的src属性显示图片
						that.musicImg = res.tempFiles[0]
						that.musicImgName = res.tempFiles[0].name
					}
				})
			}
		}
	}
</script>

<style>
	.buttonview {
		display: flex;
		flex-direction: row;
	}
</style>
